/**
 * Company : SIANGKLOM.
 * Created By : Chatree Siangklom
 * Created On : Aug 28, 2013  3:11:43 PM 
 * e-Mail : dr_chatree@hotmail.com
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : com.tis.database.dao.security.impl
 * File Name : AdUserLoginDaoImpl.java
 * TODO
 */
package com.tis.database.dao.security.impl;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import promis.utility.CommonService;

import com.tis.database.dao.impl.AbstractDaoImpl;
import com.tis.database.dao.security.AdUserLoginDao;
import com.tis.database.entitys.security.AdUserLogin;

import org.springframework.orm.hibernate4.support.*;
/**
 * @author Administrator
 *
 */
@Repository
@SuppressWarnings("unchecked")
public class AdUserLoginDaoImpl extends AbstractDaoImpl<AdUserLogin, BigDecimal> implements AdUserLoginDao {

	
	private static final Logger logging = LoggerFactory
			.getLogger(AdUserLoginDaoImpl.class);
	
	public AdUserLoginDaoImpl() {
		super(AdUserLogin.class);
	}
	
	public List<AdUserLogin> findByNameQueryAll() {
		
		return findNameQuery(AdUserLogin.findAll, null);
	}
	
	@Override
	public List<AdUserLogin> findByUserLogin(String userLogin) throws HibernateException, Exception {
		
		HashMap<String, String> parameter = new HashMap<String, String>();		
		parameter.put("userLogin", userLogin);
		
		List<AdUserLogin> list = findNameQuery(AdUserLogin.NativeSQLfindByUserLogin, parameter);

		return (CommonService.getSize(list) > 0) ? list : null;
	}

	@Override
	public List<AdUserLogin> findByFirstName(String firstName) {
		
		return findByCriteria(Restrictions.like("firstName", firstName, MatchMode.ANYWHERE));
	}
	
	@Override
	public List<AdUserLogin> findByLastedName(String lastName) {
		
		return findByCriteria(Restrictions.like("lastName", lastName, MatchMode.ANYWHERE));
	}

	@Override
	public List<AdUserLogin> findByDescription(String description) {
		
		return findByCriteria(Restrictions.like("description", description, MatchMode.ANYWHERE));
	}

	@Override
	public List<AdUserLogin> findByIsActive(String isactive) {
		
		return findByCriteria(Restrictions.like("isactive", isactive, MatchMode.ANYWHERE));
	}

	@Override
	public List<AdUserLogin> findByIsDelete(String isdeleted) {
		
		return findByCriteria(Restrictions.like("isdeleted", isdeleted, MatchMode.ANYWHERE));
	}

	@Override
	public List<AdUserLogin> findByStatus(String status) {
		
		return findByCriteria(Restrictions.like("status", status, MatchMode.ANYWHERE));
	}

	@Override	
	public AdUserLogin findByUserPassword(String user, String password)  throws HibernateException{		
		HashMap<String, String> parameter = new HashMap<String, String>();		
		parameter.put("userLogin", user);
		parameter.put("password", password);
		
		List<AdUserLogin> list = findNameQuery(AdUserLogin.NativeSQLfindByUserPassword, parameter);
		if(CommonService.getSize(list) == 1) {			
			AdUserLogin model = list.get(0);			
			return model;
		}
		else if(CommonService.getSize(list) > 1)
			throw new HibernateException("Query more than one row returned by a query [" + CommonService.getSize(list) +"] !!!");
		
		return null;
	}
	
	/** (Javadoc)
	 * @see com.tis.database.dao.impl.AbstractDaoImpl#NextSequence(java.lang.String)
	 * Date =Sep 26, 2013 : 1:24:38 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	@Override
	public BigDecimal NextSequence(String name) throws HibernateException, Exception {
		return super.NextSequence(name);
	}
	
	/** (Javadoc)
	 * @see com.tis.database.dao.impl.AbstractDaoImpl#saveOrUpdate(java.lang.Object)
	 * Date =Sep 26, 2013 : 4:12:59 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	@Override
	public void saveOrUpdate(AdUserLogin e) throws HibernateException {
		super.saveOrUpdate(e);
	}
	
	/** (Javadoc)
	 * @see com.tis.database.dao.impl.AbstractDaoImpl#save(java.lang.Object)
	 * Date =Sep 26, 2013 : 4:17:10 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	@Override
	public void save(AdUserLogin e) throws HibernateException {
		super.save(e);
	}
	
	/** (Javadoc)
	 * @see com.tis.database.dao.impl.AbstractDaoImpl#findByAll(int)
	 * Date =Oct 1, 2013 : 10:08:30 AM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	@Override
	public List<AdUserLogin> findByAll(int maxResults) throws HibernateException{
		try {
			
			Session session = getCurrentSession();
			Query q = session.getNamedQuery(AdUserLogin.findAll);
			String query = q.getQueryString();	
			logging.info(query);
			
			q = session.createQuery(query);
			if(maxResults > 0)
				q.setMaxResults(maxResults);			
			
			List<AdUserLogin> lst = Collections.checkedList(q.list(), AdUserLogin.class);
			session.flush();
			logging.info("AdUserLogin get size=" + CommonService.getSize(lst));
			
			if(CommonService.getSize(lst) > 0 )
				return lst;
			else 
				return null;
			
		}catch(HibernateException ex) {
			logging.error(ex.getMessage());
			ex.printStackTrace();
			throw ex;
		}
	}

	/** (Javadoc)
	 * @see com.tis.database.dao.security.AdUserLoginDao#StampLastedUserAccess(com.tis.database.entitys.security.AdUserLogin)
	 * Date =Sep 26, 2013 : 4:35:34 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	@Override
	public void StampLastedUserAccess(AdUserLogin e) throws HibernateException, Exception {
		try {
			
			Session session = getCurrentSession();
			String sql = " UPDATE AD_USER_LOGIN SET LASTED_ACCESS = CURRENT_DATE , LASTED_IP_ACCESS = :LASTED_IP_ACCESS_P , LASTED_ACCESS_KEY = :LASTED_ACCESS_KEY_P WHERE AD_USER_LOGIN_ID = :AD_USER_LOGIN_ID_P";
			Query query = session.createSQLQuery(sql);
			query.setParameter("LASTED_IP_ACCESS_P", e.getLastedIpAccess());
			query.setParameter("LASTED_ACCESS_KEY_P", e.getLastedAccessKey());
			query.setParameter("AD_USER_LOGIN_ID_P", e.getPkId());
			
			logging.info("sql = " + sql);
			
			query.executeUpdate();
			session.flush();
			
		}catch(Exception ex) {
			ex.printStackTrace();
			throw ex;
		}
	}
	
	/** (Javadoc)
	 * @see com.tis.database.dao.impl.AbstractDaoImpl#findById(java.io.Serializable)
	 * Date =Oct 1, 2013 : 3:00:09 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	@Override
	public AdUserLogin findById(BigDecimal id) throws HibernateException {
		return super.findById(id);
	}

	/** (Javadoc)
	 * @see com.tis.database.dao.security.AdUserLoginDao#findByAll(int, java.lang.String, java.lang.String, java.lang.String)
	 * Date =Oct 1, 2013 : 3:37:47 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	@Override
	public List<AdUserLogin> findByAll(int maxResults, String isDelete, String isActive, String status) throws HibernateException, Exception {
		try {
			
			Session session = getCurrentSession();
			Query q = session.getNamedQuery(AdUserLogin.findAll);
			String query = q.getQueryString();	
			
			if(!CommonService.IsNull(isDelete))
				query += " And upper(c.isdeleted) = '"+ isDelete.toUpperCase() +"' ";
			
			if(!CommonService.IsNull(isActive))
				query += " And upper(c.isactive) = '"+ isActive.toUpperCase() +"' ";
			
			if(!CommonService.IsNull(status))
				query += " And upper(c.status) = '"+ status.toUpperCase() +"' ";
			
			query += " order by c.pkId ASC ";
			logging.info(query);
			
			q = session.createQuery(query);
			if(maxResults > 0)
				q.setMaxResults(maxResults);			
			
			List<AdUserLogin> lst = Collections.checkedList(q.list(), AdUserLogin.class);
			session.flush();
			logging.info("AdUserLogin get size=" + CommonService.getSize(lst));
			
			if(CommonService.getSize(lst) > 0 )
				return lst;
			else 
				return null;
			
		}catch(HibernateException ex) {
			logging.error(ex.getMessage());
			ex.printStackTrace();
			throw ex;
		}
		catch(Exception ex1) {
			logging.error(ex1.getMessage());
			ex1.printStackTrace();
			throw ex1;
		}
	}
}
